package gases.as3.librarys.compnents.resource.elements
{
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	
	import gases.as3.librarys.compnents.resource.net.ResResponse;

	/**
	 * 单SWF皮肤资源单元
	 * @author guwanyuan
	 * 
	 */	
	public class UISkinResElement_SingleSWF extends BaseResElement
	{
		public function UISkinResElement_SingleSWF()
		{
			_skinResDict = new Dictionary();
			_getterDict = new Dictionary();
		}
		
		override public function getRes(name:String, reponse:ResResponse):void
		{
			var skin:*;
			if(_skinResDict[name])
			{
				skin = _skinResDict[name];
				reponse.callBack.call(null, skin);
			}
			else
			{
				if(_skinResSWF)
				{
					skin = getResByResSWF(name);
					_skinResDict[name] = skin;
					reponse.callBack.call(null, skin);
				}
				else
				{
					if(_getterDict[name] == null)
					{
						_getterDict[name] = new Array;
					}
					var list:Array = _getterDict[name] as Array;
					list.push(reponse);
				}
			}
		}
		
		/**
		 * 设置存储的皮肤请求 
		 * 
		 */		
		private function setSaveSkin():void
		{
			for(var name:String in _getterDict)
			{
				var res:* = getResByResSWF(name);
				ckeckSingleSkinRes(name,res);
				_skinResDict[name] = res;
			}
		}
		
		/**
		 * 检测单个皮肤资源, 并发送 
		 * @param name
		 * 
		 */		
		private function ckeckSingleSkinRes(name:String, res:*):void
		{
			var list:Array = _getterDict[name] as Array;
			var leng:int = list.length;
			for(var i:int = 0; i < leng; i++)
			{
				var getter:ResResponse = list[i] as ResResponse;
				getter.callBack.call(null, res);
			}
			_getterDict[name] = null;
		}
		
		/**
		 * 从SWF里反射皮肤 
		 * @param name
		 * @return 
		 * 
		 */		
		private function getResByResSWF(name:String):*
		{
			var res:* = null;
			var obj:Object = _skinResSWF.loaderInfo.applicationDomain.getDefinition(name);
			var cls:Class = obj as Class;
			res = new cls(0,0);
			return res;
		}
		
		/**
		 * 设置目标SWF 
		 * @param swf
		 * 
		 */		
		public function set skinSwf(swf:DisplayObject):void
		{
			_skinResSWF = swf;
			setSaveSkin();
		}
		
		public function setSkinResource(name:String, data:Object):void
		{
			if(_getterDict[name])
			{
				ckeckSingleSkinRes(name ,data);
			}
			_skinResDict[name] = data;
		}
		
		public function set skinResUrl(value:String):void
		{
			_skinResUrl = value;
		}
		
		public function get skinResUrl():String
		{
			return _skinResUrl;
		}
		
		override public function dispose():void
		{
			_getterDict = null;
			_skinResDict = null;
			_skinResSWF = null;
			super.dispose();
		}
		
		private var _getterDict:Dictionary;
		private var _skinResDict:Dictionary;
		private var _skinResSWF:DisplayObject;
		private var _skinResUrl:String;
	}
}




